package com.ep.general.instruktor;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;

import com.ep.general.GeneralClassMdt;
import com.ep.general.kursant.Kursant;

public class InstruktorMdt extends GeneralClassMdt<Instruktor> {

	public InstruktorMdt() {
		super(Instruktor.class);
	}

	@Override
	public Instruktor get(int id) {
		getSession().beginTransaction();
		Criteria crit = getSession().createCriteria(Instruktor.class);
		crit.add(Restrictions.eq("id", id));
		return uniqueResult(crit);
	}

	public Instruktor get(String login, String haslo) {
		getSession().beginTransaction();
		Criteria crit = getSession().createCriteria(Instruktor.class);
		crit.add(Restrictions.eq("login", login));
		crit.add(Restrictions.eq("haslo", haslo));

		return uniqueResult(crit);
	}

	public List<Instruktor> getList() {
		getSession().beginTransaction();
		Criteria crit = getSession().createCriteria(Instruktor.class);
		return list(crit);
	}

	@SuppressWarnings("unchecked")
	public List<Kursant> getList(int instruktorId) {
		getSession().beginTransaction();

		Criteria crit = getSession().createCriteria(Kursant.class);
		crit.add(Restrictions.eq("instruktor.id", instruktorId));
		List list = crit.list();

		getSession().getTransaction().commit();
		return list;
	}

	public boolean checkLoginUnique(String login) {
		getSession().beginTransaction();
		Criteria crit = getSession().createCriteria(Instruktor.class);
		crit.add(Restrictions.eq("login", login));
		return list(crit).size() == 0;
	}
}
